<?php
namespace app\admin\logic;
use app\admin\validate\WorkRecordAccount as WorkRecordAccountValidate;
use \PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class WorkRecordAccountLogic extends BaseLogic {

    /**
     * 获取日记账明细查询条件
     * @param $department
     * @param $username
     * @param $type
     * @param $income_and_expend
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function get_search_wra_maps($department,$username,$type,$income_and_expend,$start_time,$end_time){
        $maps = [];

        $department = is_array($department) ? $department : json_decode($department,true);
        if(!in_array('全部',$department)){
            $maps[] = ['department','in',$department];
        }

        if(!empty($username)){
            $u_maps = [];
            $u_maps[] = ['username','like','%' . $username . '%'];
            $openids = model('user')->get_column($u_maps,'openid');
            $maps[] = ['openid','in',$openids];
        }

        if($type != '全部'){
            $maps[] = ['type','=',$type];
        }

        if($income_and_expend != -1){
            $maps[] = ['income_and_expend','=',$income_and_expend];
        }

        if(!empty($start_time) && !empty($end_time)){
            $start_date = date('Y-m-d',strtotime($start_time));
            $end_date = date('Y-m-d',strtotime($end_time));
            $maps[] = ['now_date','between',[$start_date,$end_date]];
        }

        return $maps;
    }

    /**
     * 获取日记账明细
     * @param $params
     * @return mixed
     */
    public function get_wra($params){
        $maps = $this->get_search_wra_maps($params['department'],$params['username'],$params['type'],$params['income_and_expend'],$params['start_time'],$params['end_time']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('work_record_account')->get_list($maps,'id,openid,department,now_date,receipt_number,type,abstract,contract_number,income_and_expend,money','now_date desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            //类型及收支
            $list[$k]['income_and_expend_zw'] = $v->income_and_expend_name;
            $list[$k]['money'] = deal_number_div_100($v['money']);

            //查找用户
            $u_maps = [];
            $u_maps[] = ['openid','=',$v['openid']];
            $u_detail = model('user')->get_detail($u_maps,'id,username');
            $list[$k]['username'] = $u_detail['username'];
        }

        $total_num = model('work_record_account')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 检查修改日记账明细规则
     * @param $params
     * @return mixed
     */
    public function check_save_wra_rule($params){
        $validate = new WorkRecordAccountValidate();
        $r = $validate->scene('edit')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['id','=',$params['wraid']];
        $wra_detail =  model('work_record_account')->get_detail($maps,'id');
        if(empty($wra_detail)){
            return $this->get_response(0,'该记录不存在');
        }

        $data['wra_detail'] = $wra_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 生成日记账明细excel
     * @param $ExcelName
     * @param $data
     * @return bool
     */
    public function gen_wra_excel($ExcelName,$data){
        if(count($data[0]) > 26){
            return $this->get_response(0,'导出excel最大列数为26，请缩减数据列数');
        }
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        for($row = 1;$row <= count($data);$row++){
            $index = 0;
            foreach($data[$row-1] as $k => $v) {
                $col = chr($index + 65);
                $cell = ($col . $row);
                $sheet->setCellValue($cell, $v);
                $index++;
            }
        }

        header('Content-Type: application/vnd.ms-excel;charset=utf8-general-ci');
        header('Content-Disposition: attachment;filename='.$ExcelName);
        header('Cache-Control: max-age=0');

        $writer = new Xlsx($spreadsheet);
        $writer->save('php://output');
        //$writer->save($ExcelName);
        return true;
    }
}